package core_grpc

import (
	"fmt"
	"gitee.com/xuxinzhi2206/corehubs/nacos"
	"github.com/spf13/viper"
	"google.golang.org/grpc"
	"google.golang.org/grpc/health"
	healthpb "google.golang.org/grpc/health/grpc_health_v1"
	"log"
	"net"
)

// 匿名函数
func RegisterGRPC(hand func(s *grpc.Server)) {
	fmt.Println("建立tcp监听")
	n, er := nacos.RegisterServiceInstance(viper.GetString("ip"),
		viper.GetString("serverName"),
		viper.GetString("getGroupName"),
		viper.GetInt64("naCos_port"))
	if er != nil {
		panic(er)
	}
	fmt.Println(n)
	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", viper.GetInt64("naCos_port")))
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	hand(s)

	news := health.NewServer()
	healthpb.RegisterHealthServer(s, news)

	fmt.Println("执行！！！")
	log.Printf("server listening at %v", lis.Addr())
	if err = s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
